<template>
  <div class="container">
    <h1>新建储物室</h1>
    <hr style="border: 1px solid rgb(240, 240, 240)" />
    <span>基础信息</span>
    <div class="body">
      <div class="input">
        <div class="input-left">
          <span>楼座</span>
        </div>
        <div class="input-right">
          <el-select v-model="floor" placeholder="请选择">
            <el-option
              v-for="item in options1"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </div>
      </div>
      <div class="input">
        <div class="input-left">
          <span>单元</span>
        </div>
        <div class="input-right">
          <el-select v-model="unit" placeholder="请选择">
            <el-option
              v-for="item in options2"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </div>
      </div>
      <div class="input">
        <div class="input-left">
          <span>储物室号</span>
        </div>
        <div class="input-right">
          <el-input v-model="storage" placeholder="请输入" />
        </div>
      </div>
      <div class="input">
        <div class="input-left">
          <span>建筑面积</span>
        </div>
        <div class="input-right">
          <el-input v-model="floor_area" type="number" placeholder="请输入" />
        </div>
      </div>
      <div class="input">
        <div class="input-left">
          <span>公摊面积</span>
        </div>
        <div class="input-right">
          <el-input v-model="shared_area" type="number" placeholder="请输入" />
        </div>
      </div>
      <div class="input">
        <div class="input-left">
          <span>实用面积</span>
        </div>
        <div class="input-right">
          <el-input v-model="usable_area" type="number" placeholder="请输入" />
        </div>
      </div>
      <div class="input">
        <div class="input-left">
          <span>备注</span>
        </div>
        <div class="input-right">
          <el-input v-model="remark" :rows="5" type="textarea" />
        </div>
      </div>
      <div style="margin: 2rem 0 2rem 12rem">
        <el-button type="primary" @click="addOrUpdate">提交</el-button>
        <el-button type="default" @click="to('file/storage')">返回</el-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { onMounted, ref, watch } from "vue";
import to from "@/utils/to.js";
import axios from "axios";
import { useRoute } from "vue-router";
import { ElMessage } from "element-plus";

const route = useRoute();
const id = route.query.id;

const floor = ref("");
const unit = ref("");
const storage = ref("");
const floor_area = ref(0);
const shared_area = ref(0);
const usable_area = ref(0);
const remark = ref("");

const options1 = ref([]);
const options2 = ref([]);
const floorData = ref([]);

watch(floor, (newFloor) => {
  if (newFloor) {
    const selectedFloor = floorData.value.find(
      (floor) => floor.floorId === newFloor
    );

    if (selectedFloor) {
      options2.value = Array.from(
        { length: selectedFloor.unitNum },
        (_, index) => ({
          label: `${index + 1}单元`,
          value: `${index + 1}单元`,
        })
      );
    } else {
      options2.value = [];
    }
  }
});

const getFloor = () => {
  const config = {
    url: "http://localhost:8080/floor/get",
    method: "GET",
    headers: {
      Authorization: localStorage.getItem("token"),
    },
  };

  const response = axios(config);
  response
    .then((data) => {
      floorData.value = data.data.data;
      options1.value = floorData.value.map((item) => ({
        value: item.name,
        label: item.name,
      }));
      floorData.value = floorData.value.map((item) => ({
        floorId: item.name,
        unitNum: item.unitNum,
      }));
    })
    .catch((error) => {
      console.log(error);
    });
};

const addOrUpdate = () => {
  let config = {};

  if (id === undefined || id === "") {
    config = {
      url: "http://localhost:8080/storage/addStorage",
      method: "POST",
      headers: {
        Authorization: localStorage.getItem("token"),
      },
      data: {
        floorId: floor.value,
        unit: unit.value,
        storageNum: storage.value,
        buildAreas: floor_area.value,
        publicAreas: shared_area.value,
        relaAreas: usable_area.value,
        detail: remark.value,
      },
    };
  } else {
    config = {
      url: "http://localhost:8080/storage/updateStorage",
      method: "PUT",
      headers: {
        Authorization: localStorage.getItem("token"),
      },
      data: {
        id: id,
        floorId: floor.value,
        unit: unit.value,
        storageNum: storage.value,
        buildAreas: floor_area.value,
        publicAreas: shared_area.value,
        relaAreas: usable_area.value,
        detail: remark.value,
      },
    };
  }

  const response = axios(config);
  response
    .then((data) => {
      if (data.data.code === 1) {
        ElMessage({
          message: data.data.msg,
          type: "success",
        });
        setTimeout(() => {
          to('file/storage');
        }, 2000);
      }
    })
    .catch((error) => {
      console.log(error);
    });
};

const get = () => {
  const config = {
    url: "http://localhost:8080/storage/selectById",
    method: "GET",
    headers: {
      Authorization: localStorage.getItem("token"),
    },
    params: {
      id: id,
    },
  };

  const response = axios(config);
  response.then((data) => {
    floor.value = data.data.data.floorId;
    unit.value = data.data.data.unit;
    storage.value = data.data.data.storageNum;
    floor_area.value = data.data.data.buildAreas;
    shared_area.value = data.data.data.publicAreas;
    usable_area.value = data.data.data.relaAreas;
    remark.value = data.data.data.detail;
  });
};

onMounted(() => {
  get();
  getFloor();
});
</script>

<style scoped>
.container {
  background-color: white;
  height: auto;
  margin-top: 2rem;
  margin-bottom: 2rem;
}

h1 {
  font-size: 2rem;
  font-weight: 600;
  padding: 2rem 0 10px 4rem;
}

span {
  display: inline-block;
  font-size: 15px;
  margin: 1rem 2rem 1rem 0;
  line-height: 24px;
  width: 100px;
  text-align: right;
}

.body {
  display: grid;
  justify-content: center;
  align-items: center;
}

.input {
  display: flex;
  justify-content: center;
  align-items: center;
  width: auto;
}

.input-right {
  width: 300px;
}
</style>
